﻿using System;
using DDotNet.Data.BusinessObjects;
using DDotNet.Data.BusinessObjects.Core;
using DDotNet.Data.BusinessObjects.Validation;
using DDotNet.Data.CodeGen.Model.Entity.Abstract;

namespace DDotNet.Data.CodeGen.Model.Entity
{
    public sealed class Project : AbstractProject
    {
        #region Constructor

        /// <summary>
        /// Standard ParameterLess Constructor
        /// </summary>
        private Project()
        {
            
        }

        #endregion

        #region BusinessBase Abstract Members

        /// <summary>
        /// Override this method to return a unique identifying
        /// value for this object.
        /// </summary>
        /// <remarks>
        /// If you can not provide a unique identifying value, it
        /// is best if you can generate such a unique value (even
        /// temporarily). If you can not do that, then return 
        /// <see langword="Nothing"/> and then manually override the
        /// <see cref="BusinessBase{T}.Equals(object)"/>, <see cref="BusinessBase{T}.GetHashCode"/> and
        /// <see cref="BusinessBase{T}.ToString"/> methods in your business object.
        /// </remarks>
        protected override object GetIdValue()
        {
            return base.ID;
        }

        #endregion

        #region Validation Rules

        /// <summary>
        /// Override this method in your business class to
        /// be notified when you need to set up business
        /// rules.
        /// </summary>
        /// <remarks>
        /// AddBusinessRules is automatically called by CSLA .NET
        /// when your object should associate validation rules
        /// with its properties.
        /// </remarks>
        protected override void AddBusinessRules()
        {
            // Adiciona as Validações de Negócio
            ValidationRules.AddRule(CommonRules.StringRequired, "Name");
            ValidationRules.AddRule(CommonRules.StringMaxLength,
                                    new CommonRules.MaxLengthRuleArgs("Name", 50));
            
            // Validação para a Checagem de Usuários Relacionados
            ValidationRules.AddRule(new RuleHandler(RelatedUsersRuleHandler), "AssociatedUsers");
            
            // Continua a Adição de Validações da Base
            base.AddBusinessRules();
        }

        private bool RelatedUsersRuleHandler(object target, RuleArgs e)
        {
            // Verifica se o Projeto Contém ao Menos 1 Usuário Associado
            if (((Project)target).AssociatedUsers == null || ((Project)target).AssociatedUsers.Count == 0)
            {
                e.Description = string.Format("É necessário que existam ao menos 1 usuário associado no projeto \"{0}\"", ((Project) target).Name);
                return false;
            }
            
            return true;
        }

        #endregion
    }
}